package com.example.qixinshangwutong.model.brand;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.view.activity.CompanyActivity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class CompanyAdapte extends RecyclerView.Adapter <CompanyAdapte.MyHolder>{
    private List<CompanyModel> dataList;
    private Context context;


    public void setDataList(List<CompanyModel> dataList) {
        this.dataList = dataList;
    }

    public CompanyAdapte(Context context) {
        this.context = context;
    }

    @NonNull
    @NotNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.company_item,null));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MyHolder holder, int position) {
        if (!dataList.get(position).getUrl().isEmpty()){
            Glide.with(context).load(dataList.get(position).getUrl()).into(holder.icon);
        }else {
            holder.icon.setImageResource(R.drawable.shouye);
        }
        holder.pinpai.setText(dataList.get(position).getPinpai());
        holder.name.setText(dataList.get(position).getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, CompanyActivity.class);
                intent.putExtra("url","http://m.qxswt.com/company/"+dataList.get(position).getId());
                intent.putExtra("name",dataList.get(position).getName());
                intent.putExtra("murl",dataList.get(position).getUrl());
                context.startActivity(intent);

            }
        });


    }

    @Override
    public int getItemCount() {
        return null!=dataList?dataList.size():0;
    }
    class MyHolder extends RecyclerView.ViewHolder{
        ImageView icon;
        TextView pinpai;
        TextView name;

        public MyHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            icon=itemView.findViewById(R.id.list_icon);
            pinpai=itemView.findViewById(R.id.list_theme);
            name=itemView.findViewById(R.id.list_company);

        }
    }

}
